/*
 * FileName：PaperInfoServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.service.impl;

import javax.annotation.Resource;

import com.smart.common.Constants;
import com.smart.common.util.*;
import com.smart.common.entity.TreeNode;
import org.springframework.stereotype.Service;

import java.util.*;

import com.smart.wx.wxcms.dao.PaperInfoMapper;
import com.smart.wx.wxcms.entity.PaperInfo;
import com.smart.wx.wxcms.service.PaperInfoService;

/**
 * 问卷调查基础表
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-23 21:22:41
 */
@Service
public class PaperInfoServiceImpl implements PaperInfoService {

    @Resource
    private PaperInfoMapper paperInfoDao;

    @Override
    public PaperInfo queryObject(PaperInfo paperInfo) throws Exception {
        return paperInfoDao.queryObject(paperInfo);
    }

    @Override
    public List<PaperInfo> queryList(PaperInfo paperInfo) throws Exception {
        return paperInfoDao.queryList(paperInfo);
    }

    @Override
    public List<PaperInfo> queryListByPage(PaperInfo paperInfo) throws Exception {
        return paperInfoDao.queryListByPage(paperInfo);
    }

    @Override
    public Integer queryCount(PaperInfo paperInfo) throws Exception {
        Integer ct = paperInfoDao.queryCount(paperInfo);
        return ct == null ? 0 : ct;
    }

    @Override
    public Integer insert(PaperInfo paperInfo) throws Exception {
        return paperInfoDao.insert(paperInfo);
    }

    @Override
    public Integer update(PaperInfo paperInfo) throws Exception {
        return paperInfoDao.update(paperInfo);
    }


    @Override
    public Integer delete(PaperInfo paperInfo) throws Exception {
        String paperInfoId = paperInfo.getId();
        if (StringUtil.isNotBlank(paperInfoId)) {
            this.paperInfoDao.deleteBatch(Arrays.asList(paperInfoId.split(",")));
        }
        return 1;
    }

    @Override
    public Integer deleteBatch(List<String> ids) throws Exception {
        return paperInfoDao.deleteBatch(ids);
    }

    @Override
    public AjaxResult saveOrUpdate(PaperInfo bean) throws Exception {

        String id = bean.getId();
        if (id != null && !id.equals("")) { // 修改
            bean.setUpdateTime(new Date());
            bean.setPaperUrl(this.getPaperUrl(bean));
            this.paperInfoDao.update(bean);
        } else { // 新增
            bean.setId(Util.getUUID());
            bean.setPaperUrl(this.getPaperUrl(bean));
            bean.setCreateTime(new Date());
            bean.setUpdateTime(new Date());
            this.paperInfoDao.insert(bean);
        }
        return AjaxResult.saveSuccess();
    }

    @Override
    public List<Object> getDataTree(PaperInfo paperInfo) throws Exception {

        List<Object> resList = new ArrayList<Object>();

        List<PaperInfo> list = this.paperInfoDao.queryList(paperInfo);

        /************* 获取根节点 *****************/
        TreeNode rootNode = new TreeNode();
        rootNode.setId(Constants.ROOT_NODE_ID);
        rootNode.setParentId("-1");
        rootNode.setName(Constants.ROOT_NODE_NAME);
        //rootNode.setIsLeaf(0);
        resList.add(rootNode);

        if (list != null && list.size() > 0) {

            for (PaperInfo bean : list) {

                String id = bean.getId();
                String name = bean.getTitle();

                TreeNode node = new TreeNode();
                //node.setOpen(true);
                node.setId(id);
                node.setLevel(1);
                node.setParentId(rootNode.getId());
                node.setName(name);
                node.setNocheck(false);

                Map<String, String> attrs = new HashMap<String, String>();
                attrs.put("rootFlag", "0"); // 是否是根节点 1:是 0:不是

                node.setAttrs(attrs);
                node.setParent(false);

                resList.add(node);

            }
        }

        return resList;
    }

    /**
     * 获取问卷url
     *
     * @return
     */
    private String getPaperUrl(PaperInfo paperInfo) {
        // 微信报名页面
//        String path = SpringFreemarkerContextPathUtil.getBasePath(request);
//        String redirectUrl_apply = request.getScheme() + "://" + request.getServerName() + path + "/front/voteindex/getVoteApplyIndex/" + WxMemoryCacheClient.getAccount() + "/" + vote.getId();

        String domainName = PropertiesUtil.getString("domain.name").toString();
        String redirectUrl = domainName + "/front/paperindex/getPaperIndex/" + WxMemoryCacheClient.getAccount() + "/" + paperInfo.getId();

        return redirectUrl;
    }
}
